<?php

namespace DesignModel\decorator;
/**
 * Class Canvas
 * @description: 画一个正方形  尝试使用sql 的堆结构 和 栈结构 去实现控制反转 可以实现 太麻烦 放弃
 * @time:2018年6月20日17:42:33
 * @Author: yfl
 * @QQ 554665488
 */
class Canvas3
{
    public $data;

//    protected $decorators = array(); //用来放装饰器的盒子
    protected $heap = array();  //堆结构
    protected $stack = array();//栈结构

    public function init($width = 20, $height = 10)
    {
        $data = array();
        for ($i = 0; $i < $height; $i++) {
            for ($j = 0; $j < $width; $j++) {
                $data[$i][$j] = '*';
            }
        }
        $this->data = $data;
    }

    /**
     * @description: 用来改变画图之前的动作
     * @var void
     * Date: 2018-6-20 21:47
     */
    public function beforeDraw()
    {
//        var_dump($this->decorators, '之前');
//        foreach ($this->decorators as $decorator) {  //这里是按照先进先出原则
////            echo 'before';
//            $decorator->beforeDraw();
//        }


        var_dump($this->heap->extract());
    }

    /**
     * @description:用来改变画图之后的动作
     * @var void
     * Date: 2018-6-20 21:50
     */
    public function afterDraw()
    {
//        var_dump($this->decorators, '之后');
//        $decorators = array_reverse($this->decorators); //这里要按照使用  控制反转！！！！！ 按照先进后出 后进先出的原则 （和栈的原则一样）
//        var_dump($decorators, '之后');
//        foreach ($decorators as $decorator) {  //
////            echo 'after';
//            $decorator->afterDraw();
//        }
    }

    /**
     * @description:设置改变动作的装饰器
     * @var void
     * Date: 2018-6-20 21:52
     * @param DecoratorApi $decoratorApi
     */
    public function setDecorators(DecoratorApi $decoratorApi)
    {
//        $this->decorators[] = $decoratorApi;
        $heap = new \SplMinHeap();

        $heap->insert($decoratorApi);
        $this->heap = $heap;
         var_dump($this->heap->count());
        $stack = new \SplStack();
        $stack->push($decoratorApi);
        $this->stack = $stack;
    }

    public function draw()
    {
        $this->beforeDraw();
        foreach ($this->data as $index => $line) {
            foreach ($line as $item) {
//                echo $item;
            }
//            echo "<br />";
//            echo "\n";
        }
        $this->afterDraw();
    }

    /**
     * @description:
     * @var void
     * Date: 2018-6-20 19:53
     * @param $a1 : 开始行
     * @param $a2 :结束行
     * @param $b1 :开始列
     * @param $b2 :结束列
     */
    public function rect($a1, $a2, $b1, $b2)
    {
        foreach ($this->data as $index1 => $line) {

            if ($index1 < $a1 or $index1 > $a2) continue;
//            echo $index1."\n";
            foreach ($line as $index2 => $item) {
                if ($index2 < $b1 or $index2 > $b2) continue;
                $this->data[$index1][$index2] = '&nbsp;';
            }
        }
    }
}

//$obj = new Canvas();
//$obj->init();
//$obj->rect(3, 6, 5, 14);
//$obj->draw();